package com.mars.controller;

import com.google.common.collect.Maps;
import com.mars.beans.PageQuery;
import com.mars.beans.PageResult;
import com.mars.common.JsonData;
import com.mars.param.AclParam;
import com.mars.pojo.SysAcl;
import com.mars.pojo.SysRole;
import com.mars.pojo.SysUser;
import com.mars.service.SysAclService;
import com.mars.service.SysRoleAclService;
import com.mars.service.SysRoleService;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 权限点管理
 * @author MARS
 * @date 2018/1/17
 */

@Controller
@RequestMapping("/sys/acl")
public class SysAclController {

    @Resource
    private SysAclService sysAclService;
    @Resource
    private SysRoleService sysRoleService;

    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData saveAclModule(AclParam param){
        sysAclService.save(param);
        return JsonData.success();
    }

    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData updateAclModule(AclParam param){
        sysAclService.update(param);
        return JsonData.success();
    }

    /**
     * 根据权限模块获取权限点,分页展示
     */
    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData list (@RequestParam("aclModuleId") Integer aclModuleId, PageQuery pageQuery) {
        PageResult<SysAcl> result = sysAclService.getPageByAclModuleId(aclModuleId,pageQuery);
        return JsonData.success(result);
    }

    @RequestMapping("/acls.json")
    @ResponseBody
    public JsonData acls(@RequestParam("aclId") int aclId) {
            Map<String,Object> map = Maps.newHashMap();
            // 缓存权限已分配的角色数据
            List<SysRole> roleList = sysRoleService.getRoleListByAclId(aclId);
            // 传入权限已分配的角色
            map.put("roles",roleList);
            // 根据获取的角色列表，来得到用户
            map.put("users",sysRoleService.getUserListByRoleList(roleList));
            return JsonData.success(map);

    }
}
